#ifndef WIDGETS_LOGDOCK_H
#define WIDGETS_LOGDOCK_H

#include <QtCore/qdatetime.h>
#include <QtCore/qmutex.h>
#include <QtGui/qdockwidget.h>
#include <QtGui/qlistwidget.h>

QT_FORWARD_DECLARE_CLASS(QSpinBox)
QT_FORWARD_DECLARE_CLASS(QToolButton)

struct LogRecord
{
    QString msg;
    QtMsgType msgType;
};

class LogItem : public QListWidgetItem
{
public:
    LogItem(const QString &text, QtMsgType msgType) : QListWidgetItem()
    {
        QTime now = QTime::currentTime();
        setText("[" + now.toString("HH:mm:ss.zzz") + "] " + text);
        const QString typeIcons[] = {"info", "warning", "error", "error"};
        setIcon(QIcon(":/images/" + typeIcons[msgType] + ".png"));
        m_msgType = msgType;
    }
    QtMsgType messageType() { return m_msgType; }
private:
    QtMsgType    m_msgType;
};

class LogDock : public QDockWidget
{
    Q_OBJECT
public:
    explicit LogDock(QWidget *parent = 0);
    ~LogDock();

    static void addMessage(const QString &msg, QtMsgType msgType);

public slots:
    void clearAll();

protected:
    void timerEvent(QTimerEvent *event);

private slots:
    void filter();

private:
    void addLogItem(const LogRecord &rec);
    bool filterItem(QListWidgetItem *item);

private:
    static QList<LogRecord>  m_records;
    static QMutex            m_msgMutex;

    QToolButton             *m_enableInfo;
    QToolButton             *m_enableWarning;
    QToolButton             *m_enableError;
    QListWidget             *m_logWindow;

    int                      m_visibleCount;
    int                      m_firstVisible;
    QSpinBox                *m_msgCountFrom;
    QSpinBox                *m_msgCountTo;

};

#endif // WIDGETS_LOGDOCK_H
